package es.uji.viselab.benchmark.source;

import es.uji.viselab.network.DataPackage;
import es.uji.viselab.network.EncoderDecoder;
import es.uji.viselab.network.NetworkListener;
import es.uji.viselab.network.Server;
import es.uji.viselab.network.ServerListener;
import es.uji.viselab.network.networkServerThread;
import es.uji.viselab.network.links.VispEncoderDecoder;

public class VispSource extends Source implements NetworkListener,ServerListener {
	Server server;
	networkServerThread connection;
	
	public void start(){
		super.start();
		EncoderDecoder ed = new VispEncoderDecoder();
		server = new Server(ed,10100);
		server.addListener(this);
		Thread serverThread = new Thread(server);
		serverThread.start();
	}
	
	@Override
	public void newClient(networkServerThread nst) {
		// TODO by know, this server is only capable to serve one client at a time
		connection=nst;
		// subscribe myself for listeners, from now on, I will know when a message arrives
	    nst.addListener(this);
	
	    // Once, and only after preeliminary work has been done, I can notify everybody
		super.notifyConnectionStablished();
		}

	@Override
	public void newIncommingMessage(DataPackage dn) {
		 super.notifyDataArrival(dn);
	}

	@Override
	public void send(DataPackage dn) {
		if (connection!=null)
		connection.sendData(dn);
	}

	@Override
	public void keepReceiving() {
		connection.keepReceiving();
	}

}
